library(knitr)
knitr::opts_chunk$set(cache=T, comment=NA)
par(mar = c(1, 1, 1, 1))

## ----eval=FALSE----------------------------------------------------------
install.packages('statnet')
statnet::update_statnet()
## ------------------------------------------------------------------------
library(statnet)

help("ergm-terms")

## ------------------------------------------------------------------------
sessionInfo()

## ------------------------------------------------------------------------
set.seed(0)
library("sna")

survey.ash <- as.matrix(Ash_Cloud)
snag.ash <- network(survey.ash)
str(snag.ash)
snag.ash$names <- c("ACI", "AEA", "British CAA", "CANSO", "Commission", "Council", "Danish CAA", "EASA", "ECA", "ELFAA", "Eurocontrol", "GE", "IATA",  "ICAO", "Iceland CAA", "Iceland DCPEM", "Iceland Earth", "Iceland Met", "Irish CAA", "M?teo Fra", "Norw CAA", "P&W", "Rolls Royce", "UK Met")
node.size.ash <- setNames(c(0.78, 1.02, 1.41, 0.86, 1.3, 1.42, 1.5, 1.3, 1.05, 0.5, 1.5, 1, 1.19, 1.34, 1.25, 1.06, 1.45, 1.31, 1.21, 1.24, 1.14, 1.26, 1.27, 1.38), c("ACI", "AEA", "British CAA", "CANSO", "Commission", "Council", "Danish CAA", "EASA", "ECA", "ELFAA", "Eurocontrol", "GE", "IATA",  "ICAO", "Iceland CAA", "Iceland DCPEM", "Iceland Earth", "Iceland Met", "Irish CAA", "M?teo Fra", "Norw CAA", "P&W", "Rolls Royce", "UK Met"))
gplot(snag.ash, usearrows = T, 
      label = snag.ash$names, 
      label.pos = 9, 
      label.cex = .35, 
      label.col = "black",
      vertex.sides = c(3, 3, 4, 3, 50, 4, 4, 50, 3, 3, 50, 3, 3, 50, 6, 6, 5, 5, 4, 5, 6, 3, 3, 5),
      vertex.col = c (8, 8, 5, 8, 7, 5, 5, 7, 8, 8, 7, 8, 8, 7, 6, 6, 3, 3, 5, 3, 6, 8, 8, 3),
      edge.lwd = 0.00001,
      vertex.cex = as.matrix(node.size.ash),
      arrowhead.cex = 0.15, 
      mode = "fruchtermanreingold")

## ------------------------------------------------------------------------
gden(snag.ash, mode = "digraph")
degree(snag.ash, gmode = "digraph", cmode = "indegree")
degree(snag.ash, gmode = "digraph", cmode = "outdegree")
closeness(snag.ash, gmode = "digraph")
betweenness(snag.ash, gmode = "digraph")

snag.degcentraliz <- centralization(snag.ash, FUN = degree, mode = "digraph", cmode="indegree", normalize = T)
snag.degcentraliz

snag.closecentraliz <- centralization(snag.ash, FUN = closeness, mode = "digraph", normalize = T)
snag.closecentraliz

snag.betwencentraliz <- centralization(snag.ash, FUN = betweenness, mode = "digraph", normalize = T)
snag.betwencentraliz

cc.snag.ash <- clique.census(snag.ash)
cc.snag.ash

library(igraph)
snag.ash.igraph <- graph_from_edgelist(survey.ash, directed = TRUE)
snag.ash.igraph
mean_distance(snag.ash.igraph)
install.packages("remotes")
remotes::install_github("schochastics/igraphUtils", force = TRUE)
library(netUtils)
survey
core_periphery(snag.ash.igraph, method = "rk1_dc", iter = 500)
## ------------------------------------------------------------------------

set.seed(0)
set.vertex.attribute(snag.ash, "orgGov", value = c("Priv", "Priv", "Gov", "Priv", "Gov", "Gov", "Gov", "Gov", "Priv", "Priv", "IO", "Priv", "Priv", "IO", "Gov", "Gov", "Sci", "Sci", "Gov", "Sci", "Gov", "Priv", "Priv", "Sci"))
par(mfrow=c(2,2))

snagmodel.1 <- ergm(snag.ash ~edges + mutual + dgwesp (0.5, fixed = TRUE) 
                      + nodeifactor ('orgGov')
                      + nodeofactor ('orgGov'))
summary(snagmodel.1)

mcmc.diagnostics(snagmodel.1)
snagmodel.1.gof <- gof(snagmodel.1)
plot(snagmodel.1.gof)


snagmodel.2 <- ergm(snag.ash ~edges + mutual
                    + nodemix ('orgGov', base =c(2)))
summary(snagmodel.2)

mcmc.diagnostics(snagmodel.2)
snagmodel.2.gof <- gof(snagmodel.2)
plot(snagmodel.2.gof)
